//
//  MoreMajorVC.m
//  MyProject
//
//  Created by Mac on 2017/11/1.
//  Copyright © 2017年 yanjiaming. All rights reserved.
//

#import "MoreMajorVC.h"
#import "MoreMajorTVCell.h"
#import "GetMajorListByParamRootClass.h"

@interface MoreMajorVC ()
@property (weak, nonatomic) IBOutlet UITableView *tableView1;
@property (strong, nonatomic)  NSArray *arrData1;//图片名数组
@property (assign, nonatomic)  NSInteger selectIndex1;//

@property (weak, nonatomic) IBOutlet UITableView *tableView2;
@property (strong, nonatomic)  NSArray *arrData2;//图片名数组
@property (assign, nonatomic)  NSInteger selectIndex2;//

@property (weak, nonatomic) IBOutlet UITableView *tableView3;
@property (strong, nonatomic)  NSArray *arrData3;//图片名数组
@property (assign, nonatomic)  NSInteger selectIndex3;//
@end

@implementation MoreMajorVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"专业选择";
    self.selectIndex1 = -1;
    self.selectIndex2 = -1;
    self.selectIndex3 = -1;
    
    [self.tableView1 registerNib:[UINib nibWithNibName:@"MoreMajorTVCell" bundle:nil] forCellReuseIdentifier:@"MoreMajorTVCell1"];
    self.tableView1.tableFooterView = [[UIView alloc]init];
    
    [self.tableView2 registerNib:[UINib nibWithNibName:@"MoreMajorTVCell" bundle:nil] forCellReuseIdentifier:@"MoreMajorTVCell2"];
    self.tableView2.tableFooterView = [[UIView alloc]init];
    
    [self.tableView3 registerNib:[UINib nibWithNibName:@"MoreMajorTVCell" bundle:nil] forCellReuseIdentifier:@"MoreMajorTVCell3"];
    self.tableView3.tableFooterView = [[UIView alloc]init];
    /**获取专业列表*/
    [self requestMajoristWithLevel:1 parentMajorCode:@""];
}

/*
 方法描述:
 获取专业列表
 
 参数说明:
 <#参数说明#>
 
 返回结果:
 <#返回结果#>
 
 */
-(void)requestMajoristWithLevel:(NSInteger)level parentMajorCode:(NSString *)parentMajorCode{
    __weak typeof(self) weakSelf = self;
    [self showProgressWithView:self.view animated:YES];
    [SendRequest getMajorListByParamWithLevel:[NSString stringWithFormat:@"%zd",level] parentMajorCode:parentMajorCode result:^(NSDictionary *result, NSError *error) {
        [weakSelf hideProgress:weakSelf.view animated:YES];
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1)
        {
            GetMajorListByParamRootClass *model = [[GetMajorListByParamRootClass alloc]initWithDictionary:result[@"data"]];
            switch (level) {
                case 1:
                {
                    weakSelf.arrData1 = model.majorList;
                    [weakSelf.tableView1 reloadData];
                }
                    break;
                case 2:
                {
                    weakSelf.arrData2 = model.majorList;
                    [weakSelf.tableView2 reloadData];
                }
                    break;
                case 3:
                {
                    weakSelf.arrData3 = model.majorList;
                    [weakSelf.tableView3 reloadData];
                }
                    break;
                default:
                    break;
            }
            
        }else{
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.tableView1) {
        return self.arrData1.count;
    }else if (tableView == self.tableView2) {
        return self.arrData2.count;
    }else if (tableView == self.tableView3) {
        return self.arrData3.count;
    }else{
        return 0;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 55;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView == self.tableView1) {
        static NSString *identifier = @"MoreMajorTVCell1";
        MoreMajorTVCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        cell.selectionStyle =  UITableViewCellSelectionStyleNone;
        if (self.selectIndex1 >= 0) {
            if (self.selectIndex1 == indexPath.row) {
                cell.contentView.backgroundColor = UIColorFromRGB(0xF2F5FA);
                cell.titleLabel.textColor = UIColorFromRGB(0x1EB0C1);
                cell.indexImageView.hidden = NO;
            }else{
                cell.contentView.backgroundColor = kWhiteColor;
                cell.titleLabel.textColor = UIColorFromRGB(0x6C7374);
                cell.indexImageView.hidden = YES;
            }
        }else{
            cell.contentView.backgroundColor = kWhiteColor;
            cell.titleLabel.textColor = UIColorFromRGB(0x6C7374);
            cell.indexImageView.hidden = YES;
        }
        GetMajorListByParamMajorList *model = self.arrData1[indexPath.row];
        cell.titleLabel.text = model.majorName;
        return cell;
    }else if (tableView == self.tableView2) {
        static NSString *identifier = @"MoreMajorTVCell2";
        MoreMajorTVCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        cell.selectionStyle =  UITableViewCellSelectionStyleNone;
        if (self.selectIndex2 >= 0) {
            if (self.selectIndex2 == indexPath.row) {
                cell.contentView.backgroundColor = UIColorFromRGB(0xF2F5FA);
                cell.titleLabel.textColor = UIColorFromRGB(0x1EB0C1);
                cell.indexImageView.hidden = NO;
            }else{
                cell.contentView.backgroundColor = kWhiteColor;
                cell.titleLabel.textColor = UIColorFromRGB(0x6C7374);
                cell.indexImageView.hidden = YES;
            }
        }else{
            cell.contentView.backgroundColor = kWhiteColor;
            cell.titleLabel.textColor = UIColorFromRGB(0x6C7374);
            cell.indexImageView.hidden = YES;
        }
        GetMajorListByParamMajorList *model = self.arrData2[indexPath.row];
        cell.titleLabel.text = model.majorName;
        return cell;
    }else if (tableView == self.tableView3) {
        static NSString *identifier = @"MoreMajorTVCell3";
        MoreMajorTVCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        cell.selectionStyle =  UITableViewCellSelectionStyleNone;
        if (self.selectIndex3 >= 0) {
            if (self.selectIndex3 == indexPath.row) {
                cell.contentView.backgroundColor = UIColorFromRGB(0xF2F5FA);
                cell.titleLabel.textColor = UIColorFromRGB(0x1EB0C1);
                cell.indexImageView.hidden = NO;
            }else{
                cell.contentView.backgroundColor = kWhiteColor;
                cell.titleLabel.textColor = UIColorFromRGB(0x6C7374);
                cell.indexImageView.hidden = YES;
            }
        }else{
            cell.contentView.backgroundColor = kWhiteColor;
            cell.titleLabel.textColor = UIColorFromRGB(0x6C7374);
            cell.indexImageView.hidden = YES;
        }
        GetMajorListByParamMajorList *model = self.arrData3[indexPath.row];
        cell.titleLabel.text = model.majorName;
        return cell;
    }else{
        return nil;
    }
    
    
}

- (void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    if (tableView == self.tableView1 && self.selectIndex1 != indexPath.row) {
        self.selectIndex1 = indexPath.row;
        self.selectIndex2 = -1;
        self.selectIndex3 = -1;
        self.arrData2 = [NSMutableArray array];
        self.arrData3 = [NSMutableArray array];
        GetMajorListByParamMajorList *model = _arrData1[self.selectIndex1];
        [self requestMajoristWithLevel:2 parentMajorCode:model.majorCode];
        
    }else if (tableView == self.tableView2) {
        self.selectIndex2 = indexPath.row;
        self.selectIndex3 = -1;
        self.arrData3 = [NSMutableArray array];
        GetMajorListByParamMajorList *model = _arrData2[self.selectIndex2];
        [self requestMajoristWithLevel:3 parentMajorCode:model.majorCode];
        
    }else if (tableView == self.tableView3) {
        self.selectIndex3 = indexPath.row;
        GetMajorListByParamMajorList *model = _arrData3[self.selectIndex3];
        if (self.moreMajorSelect){
            self.moreMajorSelect(model);
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    [tableView reloadData];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
